package org.enwey.icarenew.Activity.Login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;

import org.enwey.icarenew.API.APIManager;
import org.enwey.icarenew.API.RequestResult;
import org.enwey.icarenew.APPUtils.ToastUtil;
import org.enwey.icarenew.Activity.Base.BaseActivity;
import org.enwey.icarenew.Model.User;
import org.enwey.icarenew.R;

import java.util.Hashtable;


/**
 * Created by enwey on 2014/11/23.
 */
public class ResetSetPassword extends BaseActivity {
    private EditText mVerifyCode;
    private EditText mPassword;
    private TextView mReset;
    private String phoneNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_password);
        setUp();
        findView();
        registerListener();
    }

    private void setUp() {
        showLeftBtn();
        setTitle("设置密码");
        phoneNum = getIntent().getStringExtra("phone");
    }

    private void findView() {
        mVerifyCode = (EditText) this.findViewById(R.id.activity_reset_password_verify_code);
        mPassword = (EditText) this.findViewById(R.id.activity_reset_password_password);
        mReset = (TextView) this.findViewById(R.id.activity_reset_password_btn);
    }

    private void registerListener() {
        View.OnClickListener register = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doReset();
            }
        };

        mReset.setOnClickListener(register);
    }

    private void doReset() {
        String verifyCode = mVerifyCode.getText().toString().trim();
        String password = mPassword.getText().toString().trim();

        if (TextUtils.isEmpty(verifyCode)) {
            ToastUtil.show(ResetSetPassword.this, "验证码不能为空!");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            ToastUtil.show(ResetSetPassword.this, "密码不能为空!");
            return;
        }

//        if (verifyCode.length() <= 0 || verifyCode.length() >= 10) {
//            ToastUtil.show(ResetSetPassword.this, "验证码错误!");
//            return;
//        }

        if (password.length() < 6 || password.length() > 16) {
            ToastUtil.show(ResetSetPassword.this, "密码长度为6~16个字符!");
            return;
        }

        if (TextUtils.isEmpty(phoneNum)) {
            ToastUtil.show(ResetSetPassword.this, "手机号码不能为空！");
            return;
        }
        // 回调api重设密码方法
        CallAPIResetPassword(verifyCode, password);
    }

    private void CallAPIResetPassword(String verifyCode, String password) {
        showProgressDialog().setCancelable(false);
        Hashtable parameters = new Hashtable();
        parameters.put("account",phoneNum);
        parameters.put("password",password);
        APIManager.getInstance(this).updatePW(parameters, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                hideProgressDialog();
                showException(error);
            }
        }, new Response.Listener<RequestResult<User>>() {
            @Override
            public void onResponse(RequestResult<User> response) {
                hideLoading();
                if (hasError(response)) {
                    return;
                };

                if (response != null && response.message != null) {
                    ToastUtil.show(ResetSetPassword.this, response.message);
                    goToLoginActivity(ResetSetPassword.this);
                }
            }
        });
    }

    private void goToLoginActivity(ResetSetPassword weakActivity) {
        Intent intent = new Intent();
        intent.setClass(weakActivity, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("phone", phoneNum);
        startActivity(intent);
        finish();
    }

}
